(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.document.viewController', viewController);
	viewController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','documentService' ];
	function viewController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, documentService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("cateView");
		var documentInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {			
			var params = {
					docId : documentInfo.id || documentInfo.docId
				};			
			
			return authService.view(params).then(function(response) {
				ctrl.document = response.data || document;
			}, angular.noop);
		};
	
		ctrl.viewContent = function() {
			modalService.openCustomDialogs({
				'title' : 'DeliverInfo Lookup',
				'dialogsModule' : 'cda.document',
				'params' : {
					lookupFlag : "Y"
				}, // add for not sysadm cooperation
				'btnCallback' : {
					'close' : function() {
					}
				}
			});
		};
		
		ctrl.deliver = function (){
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.deliver', args : [ctrl.document.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.deliver(ctrl.document).then(deliverSuccessful, deliverFailed);
					}
				}
			});
		};
		
		function deliverSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.deliver' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.countrygroup.list',
					'state' : 'cda.countryGroup.list',
					'params' : {}
				}]
			});
		}

		function deliverFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		
	}
})(angular);